package com.lun.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lun.domain.dto.PageDTO;
import com.lun.domain.dto.UserFormDTO;
import com.lun.domain.po.User;
import com.lun.domain.query.PageQuery;
import com.lun.domain.query.UserQuery;
import com.lun.domain.vo.UserVO;
import com.lun.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: UserController
 * Description:
 *
 */
@RequestMapping("/users")
@RestController
@Api("用户管理接口")
@RequiredArgsConstructor
public class UserController {

//    @Autowired sping不建议使用 建议使用构造方法的形式 使用longbook @RequiredArgsConstructor + final 实现自动注入
    private final IUserService userService;

    @PostMapping
    @ApiOperation("新增用户接口")
    public void saveUser(@RequestBody UserFormDTO userFormDTO){
        // 1.转换DTO为PO
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        // 2.新增
        userService.save(user);
    }

    @PostMapping("/{id}")
    @ApiOperation("删除用户接口")
    public void deleteUser(@ApiParam("用户id") @PathVariable("id") Long id) {
        userService.removeById(id);
    }
//    @GetMapping("/{id}")
//    @ApiOperation("根据id查询用户")
//    public UserVO queryUserById(@PathVariable("id") Long userId) {
//        // 1.查询用户po
//        User user = userService.getById(userId);
//        return BeanUtil.copyProperties(user, UserVO.class);
//    }

//    @GetMapping
//    @ApiOperation("根据id集合查询用户")
//    public List<UserVO> queryUserByIds(@RequestParam("ids") List<Long> ids){
//        // 1.查询用户
//        List<User> users = userService.listByIds(ids);
//        // 2.处理vo
//        return BeanUtil.copyToList(users, UserVO.class);
//    }
    @PutMapping("{id}/deduction/{money}")
    @ApiOperation("扣减用户余额")
    public void deductBalance(@PathVariable("id") Long id, @PathVariable("money")Integer money){
        userService.deductBalance(id, money);
    }

    @GetMapping("/list")
    @ApiOperation("根据id集合查询用户")
    public List<UserVO> queryUsers(UserQuery query){
        // 1.组织条件
        String username = query.getName();
        Integer status = query.getStatus();
        Integer minBalance = query.getMinBalance();
        Integer maxBalance = query.getMaxBalance();
        LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda()
                .like(username != null, User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance);
        // 2.查询用户
        List<User> users = userService.list(wrapper);
        // 3.处理vo
        return BeanUtil.copyToList(users, UserVO.class);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户")
    public UserVO queryUserById(@PathVariable("id") Long userId){
        // 基于自定义service方法查询
        return userService.queryUserAndAddressById(userId);
    }
    @GetMapping
    @ApiOperation("根据ids询用户")
    public List<UserVO> queryUserByIds(@RequestParam("ids") List<Long> ids){
        // 基于自定义service方法查询
        return userService.queryUserAndAddressByIds(ids);
    }
    @GetMapping("/page")
    @ApiOperation("根据复杂条件分页查询用户")
    public PageDTO<UserVO> queryUsersPage(UserQuery query){

        return userService.queryUserPage(query);
    }


}
